package org.example.controller;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.parser.Cookie;
import org.example.bean.LoginUser;
import org.example.bean.User;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@Slf4j
public class MainController {

    @Autowired
    User user;


    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

//    @Autowired
//    private RedisConnectionFactory redisConnectionFactory;

    @RequestMapping("/getStr")
    public String getStr(@RequestParam("name") String name, HttpServletResponse response) throws IOException {
        response.sendError(123, "出错啊");
        log.info("进入 /getStr 请求。。。");
        return "HELLO " + name;
    }

    @RequestMapping("/testRedis")
    public String testRedis() {
        log.info("进入 /geUser 请求。。。");

        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();

        LoginUser userInfo = new LoginUser("zhangsan", "123456");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 序列化
            String s = objectMapper.writeValueAsString(userInfo);
            operations.set("userInfo", s);
            String name1 = operations.get("userInfo");
            System.out.println(name1);
            // 字符串转 json
            JSONObject jsonObject = JSONObject.parseObject(name1);
            System.out.println(jsonObject);
            // 反序列化
            LoginUser loginUser = objectMapper.readValue(name1, LoginUser.class);
            System.out.println(loginUser);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        //存list
        ListOperations<String, String> listRedis = stringRedisTemplate.opsForList();
        listRedis.rightPush("nameList", "aaa");
        listRedis.rightPush("nameList", "bbb");
        listRedis.rightPush("nameList", "ccc");

        List<String> nameList = listRedis.range("nameList", 0, -1);
        System.out.println(nameList);
        return "请求成功";
    }

    @GetMapping("/testJDBC")
    public void testJDBC() {
        Long aLong = jdbcTemplate.queryForObject("select count(*) from zh_test1", Long.class);
        log.info("记录总数：{}", aLong);
    }

    @GetMapping("testMybatis")
    public JSONObject testMybatis() throws JSONException {
        JSONObject obj = new JSONObject();
//        List<Zh_test> userList = testMapper.selectList(null);
        obj.put("code", "0");
//        obj.put("data", userList);
        return obj;
    }

    @GetMapping("testData")
    public JSONObject testData() throws JSONException {
        JSONObject obj = new JSONObject();
//        Zh_test userList = testMapper.queryTestData("1");
        obj.put("code", "0");
//        obj.put("data", userList);
        return obj;
    }

    // 连表查询，查找对应用户的商品
    @GetMapping("queryUserGood")
    public JSONObject testMoreTable() throws JSONException {
        JSONObject obj = new JSONObject();
//        List<Map> res = testMapper.queryUserGood("admin");
        obj.put("code", "0");
//        obj.put("data", res);
        return obj;
    }

    // ----------------------------------------------------------------------------
    //获取链接参数 @PathVariable
    // http://localhost:8888/icbc/id/1/user/zhangsan?a=1&inters=2&inters=3
    @GetMapping("/id/{id}/user/{user}")
    public Map getDataParmas(@PathVariable("id") String id,
                             @PathVariable("user") String user,
                             @PathVariable Map<String, String> allParams,
                             @RequestHeader("User-Agent") String userAgent,
                             @RequestHeader Map<String, String> allHeaders,
                             @RequestParam("inters") List<String> inters,
                             @RequestParam Map<String, String> allGetParams,
                             @CookieValue("a") String a
    ) {
        Map<String, Object> map = new HashMap<>();
//        map.put("id", id);
//        map.put("user", user);
//        map.put("allParams", allParams);
//        map.put("userAgent", userAgent);
//        map.put("allHeaders", allHeaders);

        map.put("inters", inters);
        map.put("allGetParams", allGetParams);

        return map;
    }


}
